/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.hochschild.sgv.struts.action;

import com.hochschild.sgv.domain.Division;
import com.hochschild.sgv.domain.FiltroReporte;
import com.hochschild.sgv.domain.Reporte;
import com.hochschild.sgv.domain.Sociedad;
import com.hochschild.sgv.domain.SubDivision;
import com.hochschild.sgv.domain.Usuario;
import com.hochschild.sgv.service.EquipoManager;
import com.hochschild.sgv.service.FiltrosManager;
import com.hochschild.sgv.service.ReportePlanificacionManager;
import com.hochschild.sgv.struts.form.ReporteActionForm;
import com.hochschild.sgv.util.FechaUtil;
import com.hochschild.sgv.util.ReporteVacacionesPlanificadasExcel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author LENOVO
 */
public class ReporteAction extends DispatchAction{
    @Autowired
    private FiltrosManager filtrosManager;
    @Autowired
    private ReportePlanificacionManager reportePlanificacionManager;
    @Autowired
    private EquipoManager equipoManager;
    //REPORTE VACACIONES PLANIFICADAS
    public ActionForward cargarVacacionesPlanificadas(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<Sociedad> lstSociedad = filtrosManager.getSociedades();
        List<Division> lstDivision = new ArrayList<Division>();
        List<SubDivision> lstSubDivision = new ArrayList<SubDivision>();
        request.setAttribute("lstSociedad", lstSociedad);
        request.setAttribute("lstDivision", lstDivision);
        request.setAttribute("lstSubDivision", lstSubDivision);

        //Alterar
        HttpSession session = request.getSession();
        String DescripcionSubDivision="";
        Usuario usuario = (Usuario) session.getAttribute("session_usuario");
        System.out.println("Idusuario " + usuario.getIdUsuario() + " division: " + usuario.getDivisionUsuario() + " Subdivision: " + usuario.getSubdivision()); 

        DescripcionSubDivision= equipoManager.consultarSubDivision( usuario.getDivisionUsuario() , usuario.getSubdivision());
        System.out.println("DescripcionSubDivision: " + DescripcionSubDivision);
        request.setAttribute("esAdmin", usuario.getEsAdmin());
        request.setAttribute("subDivision", usuario.getSubdivision());
        request.setAttribute("DescripcionSubDivision",DescripcionSubDivision);
        return mapping.findForward("vacacionesPlanificadas");
    }

    public ActionForward buscarVacacionesPlanificadas(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
        ReporteActionForm reporteActionForm = (ReporteActionForm) form;

        List<Sociedad> lstSociedad = filtrosManager.getSociedades();
        List<Division> lstDivision = filtrosManager.getDivisiones(reporteActionForm.getIdSociedad());
        List<SubDivision> lstSubDivision = filtrosManager.getSubDivisiones(reporteActionForm.getIdDivision());

        //LLAMO AL SP PARA TRAER LA DATA DEL SQL
        FiltroReporte filtro = new FiltroReporte();
        filtro.setIdSociedad(reporteActionForm.getIdSociedad());
        filtro.setIdDivision(reporteActionForm.getIdDivision());
        filtro.setIdSubDivision(reporteActionForm.getIdSubDivision());
        filtro.setApellidoColaborador("%" + devuelveCadenaConComodines(reporteActionForm.getColaborador().toUpperCase().trim(),"%") + "%");
        filtro.setApellidoAprobador("%" + devuelveCadenaConComodines(reporteActionForm.getAprobador().toUpperCase().trim(),"%") + "%");
        filtro.setFechaInicio(reporteActionForm.getFechaInicio());
        filtro.setFechaFin(reporteActionForm.getFechaFin());
        
        List<Reporte> lstReporte = reportePlanificacionManager.getVacacionesPlanificadas(filtro);

        request.setAttribute("lstSociedad", lstSociedad);
        request.setAttribute("lstDivision", lstDivision);
        request.setAttribute("lstSubDivision", lstSubDivision);
        request.setAttribute("lstReporte", lstReporte);
        
         //Alterar SubDivision
            HttpSession session = request.getSession();
            String DescripcionSubDivision="";
            Usuario usuario = (Usuario) session.getAttribute("session_usuario");
            System.out.println("Idusuario " + usuario.getIdUsuario() + " division: " + usuario.getDivisionUsuario() + " Subdivision: " + usuario.getSubdivision()); 
            DescripcionSubDivision= equipoManager.consultarSubDivision( usuario.getDivisionUsuario() , usuario.getSubdivision());
            System.out.println("DescripcionSubDivision: " + DescripcionSubDivision);
            request.setAttribute("esAdmin", usuario.getEsAdmin());
            request.setAttribute("subDivision", usuario.getSubdivision());
            request.setAttribute("DescripcionSubDivision",DescripcionSubDivision);
        //Fin
            
        return mapping.findForward("vacacionesPlanificadas");
    }

     public ActionForward crearReporteExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	ReporteActionForm reporteActionForm = (ReporteActionForm) form;
        ReporteVacacionesPlanificadasExcel reporteVacacionesPlanificadasExcel = new ReporteVacacionesPlanificadasExcel();
    	HttpSession session = request.getSession();
        List<Reporte> lstReportes = (List<Reporte>)session.getAttribute("lstReporteVacacionesPlanificadas");

        HSSFWorkbook workbook = reporteVacacionesPlanificadasExcel.createWorkbook(lstReportes);
    	response.setHeader("Content-Disposition", "attachment; filename=ReporteVacacionesPlanificadas.xls");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
        return mapping.findForward("vacacionesPlanificadas");
    }

     //REPORTE SALDO DE DIAS DE VACACIONES
    public ActionForward cargarSaldoVacaciones(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<Sociedad> lstSociedad = filtrosManager.getSociedades();
        List<Division> lstDivision = new ArrayList<Division>();
        List<SubDivision> lstSubDivision = new ArrayList<SubDivision>();
        request.setAttribute("lstSociedad", lstSociedad);
        request.setAttribute("lstDivision", lstDivision);
        request.setAttribute("lstSubDivision", lstSubDivision);

        return mapping.findForward("saldoVacaciones");
    }

    public ActionForward buscarSaldoVacaciones(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
        ReporteActionForm reporteActionForm = (ReporteActionForm) form;

        List<Sociedad> lstSociedad = filtrosManager.getSociedades();
        List<Division> lstDivision = filtrosManager.getDivisiones(reporteActionForm.getIdSociedad());
        List<SubDivision> lstSubDivision = filtrosManager.getSubDivisiones(reporteActionForm.getIdDivision());

        FiltroReporte filtro = new FiltroReporte();
        filtro.setIdSociedad(reporteActionForm.getIdSociedad());
        filtro.setIdDivision(reporteActionForm.getIdDivision());
        filtro.setIdSubDivision(reporteActionForm.getIdSubDivision());
//        filtro.setApellidoColaborador(reporteActionForm.getColaborador().toUpperCase().trim());
//        filtro.setApellidoAprobador(reporteActionForm.getAprobador().toUpperCase().trim());
        filtro.setApellidoColaborador("*" + devuelveCadenaConComodines(reporteActionForm.getColaborador().toUpperCase().trim(),"*") + "*");
        filtro.setApellidoAprobador("*" + devuelveCadenaConComodines(reporteActionForm.getAprobador().toUpperCase().trim(),"*") + "*");
        
        //RFC
        List<Reporte> lstReporte = reportePlanificacionManager.getSaldoVacaciones(filtro);

        request.setAttribute("lstSociedad", lstSociedad);
        request.setAttribute("lstDivision", lstDivision);
        request.setAttribute("lstSubDivision", lstSubDivision);
        request.setAttribute("lstReporte", lstReporte);

        return mapping.findForward("saldoVacaciones");
    }

    public ActionForward crearReporteSaldoExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	ReporteActionForm reporteActionForm = (ReporteActionForm) form;
        ReporteVacacionesPlanificadasExcel reporteVacacionesPlanificadasExcel = new ReporteVacacionesPlanificadasExcel();
    	HttpSession session = request.getSession();
        List<Reporte> lstReportes = (List<Reporte>)session.getAttribute("lstReporteSaldoVacaciones");

        HSSFWorkbook workbook = reporteVacacionesPlanificadasExcel.createWorkbookSaldo(lstReportes);
    	response.setHeader("Content-Disposition", "attachment; filename=ReporteSaldoVacaciones.xls");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
        return mapping.findForward("saldoVacaciones");
    }

    //REPORTE VACACIONES REGISTRADAS
    public ActionForward cargarVacacionesRegistradas(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<Sociedad> lstSociedad = filtrosManager.getSociedades();
        List<Division> lstDivision = new ArrayList<Division>();
        List<SubDivision> lstSubDivision = new ArrayList<SubDivision>();
        request.setAttribute("lstSociedad", lstSociedad);
        request.setAttribute("lstDivision", lstDivision);
        request.setAttribute("lstSubDivision", lstSubDivision);

        return mapping.findForward("vacacionesRegistradas");
    }

    public ActionForward buscarVacacionesRegistradas(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
        ReporteActionForm reporteActionForm = (ReporteActionForm) form;
        System.out.println("buscarVacacionesRegistradas ***");
//        String colaborador="";
//        String aprobador="";
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        List<Sociedad> lstSociedad = filtrosManager.getSociedades();
        List<Division> lstDivision = filtrosManager.getDivisiones(reporteActionForm.getIdSociedad());
        List<SubDivision> lstSubDivision = filtrosManager.getSubDivisiones(reporteActionForm.getIdDivision());

        FiltroReporte filtro = new FiltroReporte();
        filtro.setIdSociedad(reporteActionForm.getIdSociedad());
        filtro.setIdDivision(reporteActionForm.getIdDivision());
        filtro.setIdSubDivision(reporteActionForm.getIdSubDivision());
//        filtro.setApellidoColaborador(reporteActionForm.getColaborador().toUpperCase().trim());
//        filtro.setApellidoAprobador(reporteActionForm.getAprobador().toUpperCase().trim());   
        filtro.setApellidoColaborador("*" + devuelveCadenaConComodines(reporteActionForm.getColaborador().toUpperCase().trim(),"*") + "*");
        filtro.setApellidoAprobador("*" + devuelveCadenaConComodines(reporteActionForm.getAprobador().toUpperCase().trim(),"*") + "*");
                        
        Date convertedDate = dateFormat.parse(reporteActionForm.getFechaInicio());
        filtro.setFechaInicio(FechaUtil.formatToStringSAP_YYYYMMDD(convertedDate));

        convertedDate = dateFormat.parse(reporteActionForm.getFechaFin());
        filtro.setFechaFin(FechaUtil.formatToStringSAP_YYYYMMDD(convertedDate));
        //RFC
        List<Reporte> lstReporte = reportePlanificacionManager.getVacacionesRegistradas(filtro);

        request.setAttribute("lstSociedad", lstSociedad);
        request.setAttribute("lstDivision", lstDivision);
        request.setAttribute("lstSubDivision", lstSubDivision);
        request.setAttribute("lstReporte", lstReporte);

        return mapping.findForward("vacacionesRegistradas");
    }

    public ActionForward crearReporteRegistradasExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	ReporteActionForm reporteActionForm = (ReporteActionForm) form;
        System.out.println("crearReporteRegistradasExcel ***");
        ReporteVacacionesPlanificadasExcel reporteVacacionesPlanificadasExcel = new ReporteVacacionesPlanificadasExcel();
    	HttpSession session = request.getSession();
        List<Reporte> lstReportes = (List<Reporte>)session.getAttribute("lstReporteVacacionesRegistradas");

        HSSFWorkbook workbook = reporteVacacionesPlanificadasExcel.createWorkbookVacacionesRegistradas(lstReportes);
    	response.setHeader("Content-Disposition", "attachment; filename=ReporteRegistroVacaciones.xls");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
        return mapping.findForward("vacacionesRegistradas");
    }

    private String devuelveCadenaConComodines(String cadena,String comodin){
        String cadenaConComodines = "";
        
        String[] arrayPalabras = cadena.split(" ");
        
        for (int i = 0; i < arrayPalabras.length - 1; i++) {
            if(!arrayPalabras[i].equals("")){
                cadenaConComodines = cadenaConComodines + arrayPalabras[i] + comodin;
            }
            
        }

        cadenaConComodines = cadenaConComodines + arrayPalabras[arrayPalabras.length - 1];
        return cadenaConComodines;
    }
    
    public ActionForward cargarVistaAnual(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
        ReporteActionForm reporteActionForm = (ReporteActionForm) form;
        
        return mapping.findForward("vistaAnual");
    }
    
    //DVQ 17122012: REPORTE DE STATUS DE PLANIFICACION
    public ActionForward cargarVacacionesStatusPlanificadas(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
        List<Sociedad> lstSociedad = filtrosManager.getSociedades();
        List<Division> lstDivision = new ArrayList<Division>();
        List<SubDivision> lstSubDivision = new ArrayList<SubDivision>();
        request.setAttribute("lstSociedad", lstSociedad);
        request.setAttribute("lstDivision", lstDivision);
        request.setAttribute("lstSubDivision", lstSubDivision);

        return mapping.findForward("vacacionesStatusPlanificadas");
    }

    public ActionForward buscarStatusPlanificadas(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
        ReporteActionForm reporteActionForm = (ReporteActionForm) form;
        System.out.println(" *** buscarStatusPlanificadas ***");
        List<Sociedad> lstSociedad = filtrosManager.getSociedades();
        List<Division> lstDivision = filtrosManager.getDivisiones(reporteActionForm.getIdSociedad());
        List<SubDivision> lstSubDivision = filtrosManager.getSubDivisiones(reporteActionForm.getIdDivision());

        FiltroReporte filtro = new FiltroReporte();
        filtro.setIdSociedad(reporteActionForm.getIdSociedad());
        filtro.setIdDivision(reporteActionForm.getIdDivision());
        filtro.setIdSubDivision(reporteActionForm.getIdSubDivision());

        if(!reporteActionForm.getColaborador().toUpperCase().trim().equals("")){
            filtro.setApellidoColaborador("%" + devuelveCadenaConComodines(reporteActionForm.getColaborador().toUpperCase().trim(),"%") + "%");
        }
         else{
            filtro.setApellidoColaborador("");
         }

        if(!reporteActionForm.getAprobador().toUpperCase().trim().equals("")){
            filtro.setApellidoAprobador("%" + devuelveCadenaConComodines(reporteActionForm.getAprobador().toUpperCase().trim(),"%") + "%");
        }
        else{
            filtro.setApellidoAprobador("");
        }
        
        
        filtro.setFechaInicio(reporteActionForm.getFechaInicio());
        filtro.setFechaFin(reporteActionForm.getFechaFin());
        filtro.setEstado(reporteActionForm.getEstado());

        //SP
        List<Reporte> lstReporte = reportePlanificacionManager.getStatusPlanificadas(filtro);

        request.setAttribute("lstSociedad", lstSociedad);
        request.setAttribute("lstDivision", lstDivision);
        request.setAttribute("lstSubDivision", lstSubDivision);
        request.setAttribute("lstReporte", lstReporte);

        return mapping.findForward("vacacionesStatusPlanificadas");
    }

    public ActionForward crearReporteStatusExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	//ReporteActionForm reporteActionForm = (ReporteActionForm) form;
        ReporteVacacionesPlanificadasExcel reporteVacacionesPlanificadasExcel = new ReporteVacacionesPlanificadasExcel();
    	HttpSession session = request.getSession();
        List<Reporte> lstReportes = (List<Reporte>)session.getAttribute("lstReporteStatusVacaciones");

        HSSFWorkbook workbook = reporteVacacionesPlanificadasExcel.createWorkbookStatus(lstReportes);
    	response.setHeader("Content-Disposition", "attachment; filename=ReporteStatusPlanificadas.xls");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
        return mapping.findForward("vacacionesStatusPlanificadas");
    } 
}